//
//  NSString+ShareJump.m
//  MFWSdk
//
//  Created by Song Xiaofeng on 13-9-13.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import "NSString+ShareJump.h"


@implementation NSString (ShareJump)
#pragma mark sharejump跳转url相关

-(NSString*)shareJumpQueryType
{
    NSString* jumpStr = self;
    __block NSString* type = @"";
    
    if (jumpStr.length > 0)
    {
        NSArray* arrayByQuestionMark = [jumpStr componentsSeparatedByString:@"?"];
        if (arrayByQuestionMark.count == 2) {
            jumpStr = [arrayByQuestionMark lastObject];
        }
        NSArray* array = [jumpStr componentsSeparatedByString:@"&"];
        if (array.count > 0)
        {
            [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                NSString* str = obj;
                NSArray* kvArray = [str componentsSeparatedByString:@"="];
                if (kvArray.count == 2) {
                    NSString* key = kvArray[0];
                    NSString* value = kvArray[1];
                    if ([key isEqualToString:@"type"] || [key isEqualToString:@"share_type"]) {
                        if (value.length > 0) {
                            type = value;
                            *stop = YES;
                        }
                    }
                }
            }];
        }
    }
    
    return type;
}

-(NSDictionary*)shareJumpQueryParams
{
    NSString* jumpStr = self;
    __block NSString* type = nil;
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:0];
    
    if (jumpStr.length > 0) {
        NSArray* array = [jumpStr componentsSeparatedByString:@"&"];
        if (array.count > 0) {
            [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                NSString* str = obj;
                NSArray* kvArray = [str componentsSeparatedByString:@"="];
                if (kvArray.count == 2) {
                    NSString* key = kvArray[0];
                    NSString* value = [kvArray[1] urlDecode];
                    if ([key isEqualToString:@"type"]
                        || [key isEqualToString:@"share_type"]) {
                        type = value;
                        [params setObject:value forKey:@"type"];
                    }else{
                        [params setObject:value forKey:key];
                    }
                }
            }];
        }
    }
    
    return params;
}

@end
